/**
 * window通用描述方法
 * 
 * @param {} idName 唯一标识
 * @param {} title 窗体标题
 * @param {} width 窗体宽度
 * @param {} height 窗体高度
 * @param {} showMaxButton 是否显示最大化按钮
 * @param {} showModal 是否显示遮罩
 * @param {} showToolbar 是否显示工具栏
 * @param {} showFooter 是否显示底部
 */
function MiniWindowPanel(idName, title, width, height, showMaxButton, showModal, 
		showToolbar, showFooter){			
	this.idName = idName;
	this.title = title;
	this.width = width;
	this.height = height;	
	this.winPanel = null;
	this.url = null;
	this.onclose = null;
	this.onmax = null;
	this.onmin = null;
	this.oncollapse = null;
	
	this.commonWin = {
		id: idName,
		title: title,
		width: width,
		height: height,
		showMaxButton: showMaxButton,
		showModal: showModal,
		showToolbar: showToolbar,
		showFooter: showFooter
	};
	
	this.winPanel = MiniuiTools.findAndCreate("Window", this.idName, this.commonWin);
	
	this.onbuttonclick = function(e){
		switch (e.name) {
			case "close":
				if (miniWin.onclose) {
					miniWin.onclose();
				}
				break;
			case "max":
				if (miniWin.onmax) {
					miniWin.onmax();
				}
				break;
			case "min":
				if (miniWin.onmin) {
					miniWin.onmin();
				}
				break;
			case "collapse":
				if (miniWin.oncollapse) {
					miniWin.oncollapse();
				}
				break;
		}
	};
	
	/**
	 * 设置关闭事件处理程序。
	 * @param close
	 * @return
	 */
	this.setOnClose = function(close) {
		this.onclose = close;
	};
	
	/**
	 * 设置最大化事件处理程序。
	 * @param max
	 * @return
	 */
	this.setOnMax = function(max) {
		this.onmax = max;
	};
	
	/**
	 * 设置最小化事件处理程序。
	 * @param min
	 * @return
	 */
	this.setOnMin = function(min) {
		this.onmin = min;
	};
	
	/**
	 * 设置折叠事件处理程序。
	 * @param collapse
	 * @return
	 */
	this.setOnCollapse = function(collapse) {
		this.oncollapse = collapse;
	};
	
	/**
	 * 获取窗体标题
	 * @return
	 */
	this.getTitle = function() {
		this.winPanel = mini.get(this.idName);
		if (this.winPanel != null) {
			return this.winPanel.getTitle();
		}
		return null;
	};
	
	/**
	 * 设置窗体标题
	 * @param {} title
	 */
	this.setTitle = function(title){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			if(title != null && typeof(title) == "string"){
				this.winPanel.setTitle(title);
			}
		}
	};
	
	/**
	 * 设置窗体宽度
	 * @param {} width
	 */
	this.setWidth = function(width){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			if(width != null && typeof(width) == "number"){
				this.winPanel.setWidth(width);
			}
		}
	};
	
	/**
	 * 设置窗体高度
	 * @param {} height
	 */
	this.setHeight = function(height){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			if(height != null && typeof(height) == "number"){
				this.winPanel.setHeight(height);
			}
		}
	};
	
	/**
	 * 设置窗体单个属性值
	 * @param {} name
	 * @param {} value
	 * 
	 * json {name : 'value'} string
	 * json {name : value} boolean number
	 */
	this.setOption = function(name, value){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			if(name != null && name != ""){
				var opt = {};
           		opt[name] = value;
           		
           		this.winPanel.set(opt);
			}
		}
	};
	
	/**
	 * 设置窗体属性列表值
	 * @param {} options 键值对
	 * 
	 *  this.options = {
	 *	    key[1]: value[1],
	 *	    key[2]: value[2],
	 *	    ...
	 *	    key[n]: value[n],
	 *  };
	 */
	this.setOptions = function(options){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.set(options);
		}
	};
	
	/**
	 * 同时设置窗体的标题、宽、高
	 * @param {} title
	 * @param {} width
	 * @param {} height
	 */
	this.setParams = function(width, height, title){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.set({width: width, height: height, title: title});
		}
	};
	
	/**
	 * 获取窗体DOM元素
	 * @return Element
	 */
	this.getEl = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel.getEl();
		}
		
		return null;
	};
	
	/**
	 * 获取窗体工具栏DOM元素
	 * @return Element
	 */
	this.getToolbarEl = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel.getToolbarEl();
		}
		
		return null;
	};
	
	/**
	 * 获取窗体内容区DOM元素
	 * @return Element
	 */
	this.getBodyEl = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel.getBodyEl();
		}
		
		return null;
	};
	
	/**
	 * 获取窗体IFrame DOM元素
	 * @return Element
	 */
	this.getIFrameEl = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel.getIFrameEl();
		}
		
		return null;
	};
	
	/**
	 * 获取窗体底部DOM元素
	 * @return Element
	 */
	this.getFooterEl = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel.getFooterEl();
		}
		
		return null;
	};
	
	/**
	 * 设置窗体内容区域的内容
	 * @param {} body
	 */
	this.setBody = function(body){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.setBody(body);
		}
	};
	
	/**
	 * 设置窗体内容区域地址
	 * @param {} url
	 */
	this.setUrl = function(url){
		this.url = url;
	}
	
	/**
	 * 加载url指定的内容区域
	 * @param {} url
	 * @param {} onLoad  加载内容时回调函数
	 * @param {} onDestroy 销毁窗体时回调函数
	 */
	this.load = function(url,onLoad,onDestroy){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.load(url,onLoad,onDestroy);
		}
	};
	
	/**
	 * 弹出显示窗体
	 * @param {} left x轴方向窗口排版，取值为（left、center、right）
	 * @param {} top y轴方向窗口排版，取值为（top、middle、bottom）
	 */
	this.show = function(left, top){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			if(left == null && top == null){
				this.winPanel.show();
			}else{
				this.winPanel.show(left, top);
			}
		}
	};
	
	/**
	 * 销毁窗体
	 */
	this.destroy = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.destroy();
		}
	};
	
	/**
	 * 最大化窗口
	 */
	this.max = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.max();
		}
	};
	
	/**
	 * 还原窗口
	 */
	this.restore  = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.restore();
		}
	};
	
	/**
	 * 清除窗体内容
	 */
	this.clear	= function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			//TODO
		}
	};
	
	/**
	 * 隐藏窗口
	 */
	this.hide = function(){
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			this.winPanel.hide();
		}
	};
	
	/**
	 * 重新创建新窗口
	 * @return {}
	 */
	this.afreshNewWin = function(){
		//重置按钮事件以避免交叉调用
		this.onclose = null;
		this.onmax = null;
		this.onmin = null;
		this.oncollapse = null;
		
		this.winPanel = mini.get(this.idName);
		if(this.winPanel != null){
			return this.winPanel;
		}else{
			return new MiniWindowPanel("miniWinId", "标题", 540, 360, false, true, false, false);
		}
	};
	
	this.setOnButtonClick = function() {
		this.winPanel.on("buttonclick", this.onbuttonclick);
	};
}

var miniWin	= new MiniWindowPanel("miniWinId", "标题", 540, 360,false, true, false, false);
//	miniWin.setOption("showCollapseButton", true);
//	miniWin.getToolbarEl().innerHTML="<h1>窗体工具栏</h1>";
//	miniWin.getFooterEl().innerHTML="<h1>窗体底部</h1>";

